use db_World

IF OBJECT_ID (N'fn_calculateInterestBySumAndMonths', N'FN') IS NOT NULL -- FN = Scalar Function, IF = Inlined Table Function, TF = Table Function
BEGIN
	DROP FUNCTION fn_calculateInterestBySumAndMonths;
END
GO

CREATE FUNCTION fn_calculateInterestBySumAndMonths(@Money MONEY, @InterestRate FLOAT, @MONTHS TINYINT)
	RETURNS MONEY 
	WITH EXECUTE AS CALLER -- Specifies the security context in which a stored procedure can be executed. In the example, the option CALLER specifies that the procedure will be executed in the context of the user that calls it. The other options that you can specify are SELF, OWNER, and user_name.
	AS
	BEGIN
		DECLARE @InterestAmount MONEY = @Money * (@InterestRate / 100)
		RETURN (@InterestAmount * @Months) + @Money
	END
GO

DECLARE @InputMoney MONEY = 1000;
DECLARE @InputInterestRate INT = 4;
DECLARE @InputMonths INT = 14;

SELECT dbo.fn_calculateInterestBySumAndMonths(@InputMoney, @InputInterestRate, @InputMonths) -- always use/execute functions with schema
GO